﻿using System.Linq;
using MSAcademic.v2.Database;
using MSAcademic.v2.Infrastructure.Article;

namespace MSAcademic.v2.Infrastructure.Picture.Impl
{
    public partial class PictureCommandsImpl:IPictureCommands
    {
        public void AddPictureToGallery(string url,int galleryId,bool isHidden,string caption)
        {
            var picture = new Database.Picture
                               {
                                   IsHidden = isHidden,
                                   GalleryId = galleryId,
                                   Caption = "",
                                   Url = url,
                                       
                               };
            using(var db = new MSAcademicDatabaseContainer())
            {
                db.Pictures.Add(picture);
                db.SaveChanges();
            }
        }
        public void EditPicture(int pictureId, string pictureCaption, bool pictureIsHidden)
        {
            using(var db= new MSAcademicDatabaseContainer())
            {
                var picture = db.Pictures.SingleOrDefault(m => m.Id == pictureId);
                if (picture != null)
                {
                    if (string.IsNullOrWhiteSpace(pictureCaption))
                    {
                        pictureCaption = "";
                    }
                    picture.Caption = pictureCaption;
                    picture.IsHidden = pictureIsHidden;
                    db.SaveChanges();
                }

            }
        }
        public void ToggleHidden(int pictureId)
        {
            using(var db= new MSAcademicDatabaseContainer())
            {
                var pic = db.Pictures.SingleOrDefault(m => m.Id == pictureId);
                if (pic == null) return;
                pic.IsHidden = !pic.IsHidden;
                db.SaveChanges();
            }
            
        }
    }
    public partial class PictureCommandsImpl
     {
        public void SetArticleThumbnailPictureUrl(int articleId,string thumbnailPicturePath)
        {
            
            using(var db= new MSAcademicDatabaseContainer())
            {
                var article = db.Articles.SingleOrDefault(m => m.Id == articleId);
                if (article != null) article.PictureUrlThumbnail =thumbnailPicturePath;
                db.SaveChanges();
            }
            
        }
        public void SetArticleNormalPictureUrl(int articleId, string normalPicturePath)
        {

            using (var db = new MSAcademicDatabaseContainer())
            {
                var article = db.Articles.SingleOrDefault(m => m.Id == articleId);
                if (article != null) article.PictueUrlNormal = normalPicturePath;
                db.SaveChanges();
            }

        }
        public void SetArticleBigPictureUrl(int articleId, string bigPicturePath)
        {

            using (var db = new MSAcademicDatabaseContainer())
            {
                var article = db.Articles.SingleOrDefault(m => m.Id == articleId);
                if (article != null) article.PictureUrlBig = bigPicturePath;
                db.SaveChanges();
            }

        }

        public void SetUserProfilePicture(int userId,string path)
        {
            using(var db= new MSAcademicDatabaseContainer())
            {
                var user = db.Users.SingleOrDefault(m => m.Id == userId);
                if (user == null) return;

                user.ProfilePictureUrl = path;
                db.SaveChanges();
            }
        }

     
     }
    public partial class PictureCommandsImpl
    {
        public IArticleQueries ArticleQueries { get; set; }
        public PictureCommandsImpl(IArticleQueries articleQueries)
        {
            ArticleQueries = articleQueries;
        }

    }
}
        